package com.wcpdoc.exam.core.dao.impl;

import com.wcpdoc.core.dao.impl.RBaseDaoImpl;
import com.wcpdoc.core.entity.PageIn;
import com.wcpdoc.core.entity.PageOut;
import com.wcpdoc.core.util.SqlUtil;
import com.wcpdoc.exam.core.dao.MyCollectDao;
import com.wcpdoc.exam.core.entity.MyCollect;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 练习数据访问层实现
 * 
 */
@Repository
public class MyCollectDaoImpl extends RBaseDaoImpl<MyCollect> implements MyCollectDao {
//    @Resource
//    private UserDao userDao;
	
	@Override
	public PageOut getListpage(PageIn pageIn) {
		String sql = "SELECT QUESTION_TYPE.ID AS ID, "
				+ "QUESTION_TYPE.NAME AS QUESTION_TYPE_NAME, "
				+ "GROUP_T.COUNT AS QUESTION_NUM "
				+ "FROM (SELECT QUESTION_TYPE_ID,COUNT(1) COUNT FROM EXM_MY_QUESTION_COLLECT WHERE TYPE="+pageIn.get("type", Integer.class)+" AND USER_ID="+pageIn.get("curUserId", Integer.class)+" GROUP BY QUESTION_TYPE_ID) GROUP_T "
				+ "INNER JOIN EXM_QUESTION_TYPE QUESTION_TYPE ON GROUP_T.QUESTION_TYPE_ID=QUESTION_TYPE.ID "
				+" ORDER BY QUESTION_TYPE.SORT ASC ";
		SqlUtil sqlUtil = new SqlUtil(sql);
		PageOut pageOut = getListpage(sqlUtil, pageIn);
		return pageOut;
	}

	@Override
	public List<MyCollect> getList(Integer questionTypeId) {
		String sql = "SELECT * FROM EXM_MY_QUESTION_COLLECT WHERE QUESTION_TYPE_ID = :QUESTION_TYPE_ID ";
		return getList(sql, new Object[] { questionTypeId });
	}

	@Override
	public MyCollect getByQuestion(Integer questionId, Integer userId, Integer type) {
		String sql = "SELECT * FROM EXM_MY_QUESTION_COLLECT WHERE QUESTION_ID = :QUESTION_ID AND USER_ID = :USER_ID AND TYPE = :TYPE";
		return getEntity(sql, new Object[] { questionId,userId,type });
	}

	@Override
	public void delByQuestionId(Integer questionId, Integer userId, Integer type) {
		String sql = "DELETE FROM EXM_MY_QUESTION_COLLECT WHERE QUESTION_ID = :QUESTION_ID AND USER_ID = :USER_ID AND TYPE = :TYPE";
		delBy(sql,new Object[]{questionId,userId,type});
	}

	@Override
	public List<MyCollect> getIds(Integer questionTypeId,Integer type, Integer userId) {
		String sql = "SELECT * FROM EXM_MY_QUESTION_COLLECT WHERE QUESTION_TYPE_ID = :QUESTION_TYPE_ID AND TYPE = :TYPE AND USER_ID = :USER_ID ";
//		List<Map<String, Object>> mapList = getMapList(sql, new Object[] { questionTypeId,type,userId });
//		List<Integer> list = new ArrayList<>(mapList.size());
//		for (Map<String, Object> map : mapList) {
//			list.add((Integer)map.get("questionId"));
//		}
//		return list;
		return getList(sql, new Object[] { questionTypeId,type,userId });
	}

	/**
	 * 收藏试题
	 *
	 * @return
	 */
	@Override
	public List<Map<String,Object>> getPaperByType(Integer type,Integer userId,Integer questionTypeId) {
		String sql ="select a.*,b.ANSWER,c.id as collectId " +
				"from exm_question a " +
				"inner join exm_question_answer b on a.id=b.QUESTION_ID " +
				"inner join exm_my_question_collect c on a.ID=c.QUESTION_ID and c.TYPE="+type+" and c.USER_ID="+userId+" "+
				"where a.STATE=1 AND a.QUESTION_TYPE_ID="+questionTypeId+"";
		return getMapList(sql);
	}

}